Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 | 'use client' import dynamic from 'next/dynamic' import { useCallback, useEffect, useRef, useState } from 'react' import { useDocumentDetection } from '@/components/practice/useDocumentDetection' import { ScannerControlsDrawer } from '@/components/practice/ScannerControlsDrawer' import { useScannerSettings, useUpdateScannerSettings } from '@/hooks/useScannerSettings' import type { QuadDetectorConfig } from '@/lib/vision/quadDetector' import { css } from '../../../../../styled-system/css' // Dynamic import for DocumentAdjuster (pulls in OpenCV) const DocumentAdjuster = dynamic( () => import('@/components/practice/DocumentAdjuster').then((m) => m.DocumentAdjuster), { ssr: false } ) // Lighting presets for different conditions const LIGHTING_PRESETS = { normal: { label: 'Normal', icon: '☀️', config: { preprocessing: 'multi' as const, enableHistogramEqualization: true, enableAdaptiveThreshold: true, enableMorphGradient: true, cannyThresholds: [50, 150] as [number, number], adaptiveBlockSize: 11, adaptiveC: 2, }, }, lowLight: { label: 'Low Light', icon: '🌙', config: { preprocessing: 'multi' as const, enableHistogramEqualization: true, enableAdaptiveThreshold: true, enableMorphGradient: true, cannyThresholds: [30, 100] as [number, number], adaptiveBlockSize: 15, adaptiveC: 5, }, }, bright: { label: 'Bright', icon: '🔆', config: { preprocessing: 'enhanced' as const, enableHistogramEqualization: true, enableAdaptiveThreshold: false, enableMorphGradient: false, cannyThresholds: [80, 200] as [number, number], adaptiveBlockSize: 11, adaptiveC: 2, }, }, } // Analyze video frame to determine best preset function analyzeFrameLighting(video: HTMLVideoElement): { preset: 'normal' | 'lowLight' | 'bright' brightness: number contrast: number } { const canvas = document.createElement('canvas') const ctx = canvas.getContext('2d') if (!ctx) return { preset: 'normal', brightness: 128, contrast: 50 } // Sample at lower resolution for speed const sampleWidth = 160 const sampleHeight = 120 canvas.width = sampleWidth canvas.height = sampleHeight ctx.drawImage(video, 0, 0, sampleWidth, sampleHeight) const imageData = ctx.getImageData(0, 0, sampleWidth, sampleHeight) const data = imageData.data // Calculate luminance for each pixel (simple average) let totalLuminance = 0 const luminances: number[] = [] const pixelCount = data.length / 4 for (let i = 0; i < data.length; i += 4) { // Weighted luminance: 0.299*R + 0.587*G + 0.114*B const luminance = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2] luminances.push(luminance) totalLuminance += luminance } const avgBrightness = totalLuminance / pixelCount // Calculate contrast (standard deviation) let varianceSum = 0 for (const lum of luminances) { varianceSum += (lum - avgBrightness) ** 2 } const contrast = Math.sqrt(varianceSum / pixelCount) // Determine preset based on metrics let preset: 'normal' | 'lowLight' | 'bright' = 'normal' if (avgBrightness < 70) { // Dark scene preset = 'lowLight' } else if (avgBrightness > 180 && contrast < 40) { // Bright and washed out preset = 'bright' } else if (avgBrightness > 160) { // Just bright preset = 'bright' } // Otherwise normal return { preset, brightness: avgBrightness, contrast } } interface FullscreenCameraProps { /** Called with cropped file, original file, corners, and rotation for later re-editing */ onCapture: ( croppedFile: File, originalFile: File, corners: Array<{ x: number; y: number }>, rotation: 0 | 90 | 180 | 270 ) => void onClose: () => void } export function FullscreenCamera({ onCapture, onClose }: FullscreenCameraProps) { const videoRef = useRef<HTMLVideoElement>(null) const canvasRef = useRef<HTMLCanvasElement>(null) const overlayCanvasRef = useRef<HTMLCanvasElement>(null) const streamRef = useRef<MediaStream | null>(null) const animationFrameRef = useRef<number | null>(null) const autoCaptureTriggeredRef = useRef(false) const [isReady, setIsReady] = useState(false) const [error, setError] = useState<string | null>(null) const [isCapturing, setIsCapturing] = useState(false) const [documentDetected, setDocumentDetected] = useState(false) // Camera controls state const [torchAvailable, setTorchAvailable] = useState(false) const [torchOn, setTorchOn] = useState(false) const [hasMultipleCameras, setHasMultipleCameras] = useState(false) const [facingMode, setFacingMode] = useState<'environment' | 'user'>('environment') // Preset selector state const [presetMode, setPresetMode] = useState<'auto' | 'normal' | 'lowLight' | 'bright'>('auto') const [detectedPreset, setDetectedPreset] = useState<'normal' | 'lowLight' | 'bright'>('normal') const [presetPopoverOpen, setPresetPopoverOpen] = useState(false) const [fingerOcclusionMode, setFingerOcclusionMode] = useState(false) // Adjustment mode state const [adjustmentMode, setAdjustmentMode] = useState<{ sourceCanvas: HTMLCanvasElement corners: Array<{ x: number; y: number }> } | null>(null) // Advanced controls drawer state const [isDrawerOpen, setIsDrawerOpen] = useState(false) const drawerSwipeRef = useRef<{ startX: number; startY: number } | null>(null) const settingsInitializedRef = useRef(false) // Load persisted scanner settings from database const { data: savedSettings } = useScannerSettings() const updateSettingsMutation = useUpdateScannerSettings() // Document detection hook (lazy loads OpenCV.js) const { isLoading: isScannerLoading, isReady: isScannerReady, ensureOpenCVLoaded, isStable: isDetectionStable, isLocked: isDetectionLocked, debugInfo: scannerDebugInfo, cv: opencvRef, getBestQuadCorners, captureSourceFrame, highlightDocument, detectQuadsInImage: detectQuadsInCamera, resetTracking, updateDetectorConfig, detectorConfig, } = useDocumentDetection() // Initialize detector config with saved settings when they load useEffect(() => { if (savedSettings && !settingsInitializedRef.current) { settingsInitializedRef.current = true updateDetectorConfig(savedSettings) } }, [savedSettings, updateDetectorConfig]) // Handle config changes - update both local state and persist to database const handleConfigChange = useCallback( (newConfig: Partial<QuadDetectorConfig>) => { // Update local detector immediately for instant feedback updateDetectorConfig(newConfig) // Persist to database (uses optimistic update) updateSettingsMutation.mutate(newConfig) }, [updateDetectorConfig, updateSettingsMutation] ) // Apply preset config when preset mode or finger occlusion changes const applyPresetConfig = useCallback( (presetKey: 'normal' | 'lowLight' | 'bright') => { const preset = LIGHTING_PRESETS[presetKey] const config: Partial<QuadDetectorConfig> = { ...preset.config } // Add finger occlusion enhancements if enabled if (fingerOcclusionMode) { config.enableHoughLines = true config.enableMorphGradient = true // Slightly lower thresholds to catch partial edges const baseThresholds = preset.config.cannyThresholds config.cannyThresholds = [ Math.max(20, baseThresholds[0] - 15), Math.max(80, baseThresholds[1] - 30), ] as [number, number] } updateDetectorConfig(config) }, [fingerOcclusionMode, updateDetectorConfig] ) // Analyze frame periodically when in auto mode useEffect(() => { if (presetMode !== 'auto' || !isReady || !videoRef.current || adjustmentMode) return const analyzeInterval = setInterval(() => { if (videoRef.current) { const analysis = analyzeFrameLighting(videoRef.current) if (analysis.preset !== detectedPreset) { setDetectedPreset(analysis.preset) applyPresetConfig(analysis.preset) } } }, 2000) // Analyze every 2 seconds // Initial analysis const analysis = analyzeFrameLighting(videoRef.current) setDetectedPreset(analysis.preset) applyPresetConfig(analysis.preset) return () => clearInterval(analyzeInterval) }, [presetMode, isReady, adjustmentMode, detectedPreset, applyPresetConfig]) // Apply preset when manually selected useEffect(() => { if (presetMode !== 'auto') { applyPresetConfig(presetMode) } }, [presetMode, applyPresetConfig]) // Reapply current preset when finger occlusion mode changes useEffect(() => { const currentPreset = presetMode === 'auto' ? detectedPreset : presetMode applyPresetConfig(currentPreset) }, [fingerOcclusionMode, presetMode, detectedPreset, applyPresetConfig]) // Check for multiple cameras on mount useEffect(() => { const checkCameras = async () => { try { const devices = await navigator.mediaDevices.enumerateDevices() const videoDevices = devices.filter((d) => d.kind === 'videoinput') setHasMultipleCameras(videoDevices.length > 1) } catch { // Ignore errors - just won't show flip button } } checkCameras() }, []) useEffect(() => { let cancelled = false const startCamera = async () => { try { // Start loading OpenCV in parallel with camera setup // (this component requires OpenCV for document detection) const opencvPromise = ensureOpenCVLoaded() const constraints: MediaStreamConstraints = { video: { facingMode: { ideal: facingMode }, width: { ideal: 1920 }, height: { ideal: 1080 }, }, audio: false, } const stream = await navigator.mediaDevices.getUserMedia(constraints) if (cancelled) { stream.getTracks().forEach((track) => track.stop()) return } streamRef.current = stream // Check for torch capability const videoTrack = stream.getVideoTracks()[0] if (videoTrack) { const capabilities = videoTrack.getCapabilities?.() // @ts-expect-error - torch is not in the standard types but exists on mobile const hasTorch = capabilities?.torch === true setTorchAvailable(hasTorch) setTorchOn(false) // Reset torch state when camera changes } if (videoRef.current) { videoRef.current.srcObject = stream await videoRef.current.play() if (!cancelled) { setIsReady(true) } } // Wait for OpenCV to finish loading (should already be done or almost done) await opencvPromise } catch (err) { if (cancelled) return console.error('Camera access error:', err) setError('Camera access denied. Please allow camera access and try again.') } } startCamera() return () => { cancelled = true if (streamRef.current) { streamRef.current.getTracks().forEach((track) => track.stop()) streamRef.current = null } } }, [ensureOpenCVLoaded, facingMode]) // Detection loop - runs when camera and scanner are ready, and NOT in adjustment mode useEffect(() => { // Don't run detection loop while in adjustment mode if (!isReady || !isScannerReady || adjustmentMode) return const video = videoRef.current const overlay = overlayCanvasRef.current if (!video || !overlay) return // Sync overlay canvas size with video const syncCanvasSize = () => { if (overlay && video) { overlay.width = video.clientWidth overlay.height = video.clientHeight } } syncCanvasSize() const detectLoop = () => { // Run detection every frame for smooth tracking // Detection typically takes ~8-10ms, well within 60fps budget if (video && overlay) { const detected = highlightDocument(video, overlay) setDocumentDetected(detected) } animationFrameRef.current = requestAnimationFrame(detectLoop) } // Start detection loop animationFrameRef.current = requestAnimationFrame(detectLoop) // Sync on resize window.addEventListener('resize', syncCanvasSize) return () => { if (animationFrameRef.current) { cancelAnimationFrame(animationFrameRef.current) animationFrameRef.current = null } window.removeEventListener('resize', syncCanvasSize) } }, [isReady, isScannerReady, highlightDocument, adjustmentMode]) // Enter adjustment mode with captured frame and detected corners // Always shows the adjustment UI - uses fallback corners if no quad detected const enterAdjustmentMode = useCallback(() => { if (!videoRef.current) return const video = videoRef.current const sourceCanvas = captureSourceFrame(video) const detectedCorners = getBestQuadCorners() if (!sourceCanvas) return // Stop detection loop while adjusting if (animationFrameRef.current) { cancelAnimationFrame(animationFrameRef.current) animationFrameRef.current = null } // Use detected corners if available, otherwise use full image bounds as fallback // This allows user to manually define crop area even when detection fails const corners = detectedCorners || [ { x: 0, y: 0 }, { x: sourceCanvas.width, y: 0 }, { x: sourceCanvas.width, y: sourceCanvas.height }, { x: 0, y: sourceCanvas.height }, ] setAdjustmentMode({ sourceCanvas, corners }) }, [captureSourceFrame, getBestQuadCorners]) // Handle capture button - enters adjustment mode if document detected const capturePhoto = () => { if (isCapturing) return setIsCapturing(true) enterAdjustmentMode() setIsCapturing(false) } // Auto-capture when detection is locked and stable useEffect(() => { if ( isDetectionLocked && isReady && isScannerReady && !isCapturing && !adjustmentMode && !autoCaptureTriggeredRef.current ) { // Add a small delay to ensure stability const timeout = setTimeout(() => { if (isDetectionLocked && !autoCaptureTriggeredRef.current) { autoCaptureTriggeredRef.current = true console.log('Auto-capturing document...') enterAdjustmentMode() } }, 500) // 500ms delay after lock to ensure stability return () => clearTimeout(timeout) } }, [isDetectionLocked, isReady, isScannerReady, isCapturing, adjustmentMode, enterAdjustmentMode]) // Handle adjustment confirm - pass cropped file, original file, corners, and rotation for later re-editing const handleAdjustmentConfirm = useCallback( async ( croppedFile: File, corners: Array<{ x: number; y: number }>, rotation: 0 | 90 | 180 | 270 ) => { if (!adjustmentMode) return // Convert source canvas to file for original preservation const originalBlob = await new Promise<Blob>((resolve, reject) => { adjustmentMode.sourceCanvas.toBlob( (b) => { if (b) resolve(b) else reject(new Error('Failed to create original blob')) }, 'image/jpeg', 0.95 ) }) const originalFile = new File([originalBlob], `original-${Date.now()}.jpg`, { type: 'image/jpeg', }) setAdjustmentMode(null) onCapture(croppedFile, originalFile, corners, rotation) }, [onCapture, adjustmentMode] ) // Handle adjustment cancel - return to camera // The detection loop will automatically restart via useEffect when adjustmentMode becomes null const handleAdjustmentCancel = useCallback(() => { setAdjustmentMode(null) autoCaptureTriggeredRef.current = false // Allow auto-capture again resetTracking() // Clear old quad detection state }, [resetTracking]) // Show adjustment UI if in adjustment mode (overlay on top of camera) // Keep camera mounted but hidden to preserve video stream const showAdjuster = !!(adjustmentMode && opencvRef) // Toggle torch/flashlight const toggleTorch = useCallback(async () => { if (!streamRef.current || !torchAvailable) return const videoTrack = streamRef.current.getVideoTracks()[0] if (!videoTrack) return try { const newTorchState = !torchOn await videoTrack.applyConstraints({ // @ts-expect-error - advanced torch constraint exists on mobile advanced: [{ torch: newTorchState }], }) setTorchOn(newTorchState) } catch (err) { console.error('Failed to toggle torch:', err) } }, [torchAvailable, torchOn]) // Flip between front and back camera const flipCamera = useCallback(() => { // Reset tracking and torch state before switching resetTracking() setTorchOn(false) setIsReady(false) setFacingMode((prev) => (prev === 'environment' ? 'user' : 'environment')) }, [resetTracking]) // Swipe handlers for drawer const handleTouchStart = useCallback((e: React.TouchEvent) => { const touch = e.touches[0] // Only track swipes starting from left edge (first 30px) if (touch.clientX < 30) { drawerSwipeRef.current = { startX: touch.clientX, startY: touch.clientY } } }, []) const handleTouchMove = useCallback((e: React.TouchEvent) => { if (!drawerSwipeRef.current) return const touch = e.touches[0] const deltaX = touch.clientX - drawerSwipeRef.current.startX const deltaY = Math.abs(touch.clientY - drawerSwipeRef.current.startY) // If swiping right and more horizontal than vertical, open drawer if (deltaX > 50 && deltaX > deltaY * 2) { setIsDrawerOpen(true) drawerSwipeRef.current = null } }, []) const handleTouchEnd = useCallback(() => { drawerSwipeRef.current = null }, []) return ( <div data-component="fullscreen-camera" onTouchStart={handleTouchStart} onTouchMove={handleTouchMove} onTouchEnd={handleTouchEnd} className={css({ position: 'absolute', inset: 0, bg: 'black', })} > {/* Always render video to keep stream alive */} <video ref={videoRef} playsInline muted className={css({ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover', // Hide when in adjustment mode but keep mounted display: showAdjuster ? 'none' : 'block', })} /> {/* Document adjuster overlay */} {showAdjuster && ( <DocumentAdjuster sourceCanvas={adjustmentMode.sourceCanvas} initialCorners={adjustmentMode.corners} onConfirm={handleAdjustmentConfirm} onCancel={handleAdjustmentCancel} cv={opencvRef} detectQuadsInImage={detectQuadsInCamera} /> )} {/* Camera UI - hidden when adjuster is shown */} {!showAdjuster && ( <> {/* Overlay canvas for document detection visualization */} <canvas ref={overlayCanvasRef} className={css({ position: 'absolute', inset: 0, width: '100%', height: '100%', pointerEvents: 'none', })} /> <canvas ref={canvasRef} style={{ display: 'none' }} /> </> )} {!showAdjuster && !isReady && !error && ( <div className={css({ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', bg: 'black', })} > <div className={css({ color: 'white', fontSize: 'xl' })}>Starting camera...</div> </div> )} {!showAdjuster && error && ( <div className={css({ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', bg: 'black', p: 6, })} > <div className={css({ color: 'red.400', fontSize: 'lg', textAlign: 'center', mb: 4, })} > {error} </div> <button type="button" onClick={onClose} className={css({ px: 6, py: 3, bg: 'white', color: 'black', borderRadius: 'full', fontSize: 'lg', fontWeight: 'bold', cursor: 'pointer', })} > Close </button> </div> )} {!showAdjuster && !error && ( <> {/* Top right controls */} <div data-element="top-controls" className={css({ position: 'absolute', top: 4, right: 4, display: 'flex', gap: 2, zIndex: 10, })} > {/* Preset selector with popover */} <div className={css({ position: 'relative' })}> <button type="button" onClick={() => setPresetPopoverOpen(!presetPopoverOpen)} className={css({ height: '44px', px: 3, bg: 'rgba(0, 0, 0, 0.5)', color: 'white', borderRadius: 'full', fontSize: 'sm', cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 1.5, backdropFilter: 'blur(4px)', _hover: { bg: 'rgba(0, 0, 0, 0.7)' }, })} title="Lighting preset" > <span> {presetMode === 'auto' ? `🅰️ ${LIGHTING_PRESETS[detectedPreset].icon}` : LIGHTING_PRESETS[presetMode].icon} </span> <span className={css({ fontSize: 'xs' })}> {presetMode === 'auto' ? 'Auto' : LIGHTING_PRESETS[presetMode].label} </span> </button> {/* Popover */} {presetPopoverOpen && ( <> {/* Backdrop to close popover */} <div onClick={() => setPresetPopoverOpen(false)} className={css({ position: 'fixed', inset: 0, zIndex: 5, })} /> <div data-element="preset-popover" className={css({ position: 'absolute', top: '100%', right: 0, mt: 2, bg: 'rgba(0, 0, 0, 0.9)', backdropFilter: 'blur(8px)', borderRadius: 'lg', border: '1px solid', borderColor: 'rgba(255, 255, 255, 0.2)', overflow: 'hidden', zIndex: 10, minWidth: '140px', })} > {/* Auto option */} <button type="button" onClick={() => { setPresetMode('auto') setPresetPopoverOpen(false) }} className={css({ width: '100%', px: 3, py: 2, display: 'flex', alignItems: 'center', gap: 2, bg: presetMode === 'auto' ? 'rgba(59, 130, 246, 0.3)' : 'transparent', color: 'white', fontSize: 'sm', cursor: 'pointer', _hover: { bg: 'rgba(255, 255, 255, 0.1)' }, })} > <span>🅰️</span> <span>Auto</span> {presetMode === 'auto' && ( <span className={css({ ml: 'auto', fontSize: 'xs', color: 'blue.400', })} > ✓ </span> )} </button> {/* Divider */} <div className={css({ h: '1px', bg: 'rgba(255, 255, 255, 0.1)', })} /> {/* Preset options */} {(Object.keys(LIGHTING_PRESETS) as Array<keyof typeof LIGHTING_PRESETS>).map( (key) => ( <button type="button" key={key} onClick={() => { setPresetMode(key) setPresetPopoverOpen(false) }} className={css({ width: '100%', px: 3, py: 2, display: 'flex', alignItems: 'center', gap: 2, bg: presetMode === key ? 'rgba(59, 130, 246, 0.3)' : 'transparent', color: 'white', fontSize: 'sm', cursor: 'pointer', _hover: { bg: 'rgba(255, 255, 255, 0.1)' }, })} > <span>{LIGHTING_PRESETS[key].icon}</span> <span>{LIGHTING_PRESETS[key].label}</span> {presetMode === key && ( <span className={css({ ml: 'auto', fontSize: 'xs', color: 'blue.400', })} > ✓ </span> )} {presetMode === 'auto' && detectedPreset === key && ( <span className={css({ ml: 'auto', fontSize: '9px', color: 'gray.400', })} > detected </span> )} </button> ) )} </div> </> )} </div> {/* Finger occlusion mode toggle */} <button type="button" onClick={() => setFingerOcclusionMode(!fingerOcclusionMode)} className={css({ width: '44px', height: '44px', bg: fingerOcclusionMode ? 'purple.600' : 'rgba(0, 0, 0, 0.5)', color: 'white', borderRadius: 'full', fontSize: 'lg', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', backdropFilter: 'blur(4px)', transition: 'all 0.2s', _hover: { bg: fingerOcclusionMode ? 'purple.500' : 'rgba(0, 0, 0, 0.7)', }, })} title={fingerOcclusionMode ? 'Finger mode ON' : 'Finger mode OFF (experimental)'} > 👆 </button> {/* Torch toggle - only shown when available */} {torchAvailable && ( <button type="button" onClick={toggleTorch} className={css({ width: '44px', height: '44px', bg: torchOn ? 'yellow.400' : 'rgba(0, 0, 0, 0.5)', color: torchOn ? 'black' : 'white', borderRadius: 'full', fontSize: 'xl', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', backdropFilter: 'blur(4px)', transition: 'all 0.2s', _hover: { bg: torchOn ? 'yellow.300' : 'rgba(0, 0, 0, 0.7)' }, })} title={torchOn ? 'Turn off flashlight' : 'Turn on flashlight'} > {torchOn ? '🔦' : '💡'} </button> )} {/* Camera flip - only shown when multiple cameras */} {hasMultipleCameras && ( <button type="button" onClick={flipCamera} className={css({ width: '44px', height: '44px', bg: 'rgba(0, 0, 0, 0.5)', color: 'white', borderRadius: 'full', fontSize: 'xl', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', backdropFilter: 'blur(4px)', _hover: { bg: 'rgba(0, 0, 0, 0.7)' }, })} title="Flip camera" > 🔄 </button> )} {/* Close button */} <button type="button" onClick={onClose} className={css({ width: '44px', height: '44px', bg: 'rgba(0, 0, 0, 0.5)', color: 'white', borderRadius: 'full', fontSize: '2xl', fontWeight: 'bold', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', backdropFilter: 'blur(4px)', _hover: { bg: 'rgba(0, 0, 0, 0.7)' }, })} > × </button> </div> {/* Debug overlay panel - always shown to help diagnose detection */} <div data-element="scanner-debug-panel" className={css({ position: 'absolute', top: 4, left: 4, p: 3, bg: 'rgba(0, 0, 0, 0.8)', backdropFilter: 'blur(4px)', borderRadius: 'lg', color: 'white', fontSize: 'xs', fontFamily: 'monospace', maxWidth: '280px', zIndex: 10, })} > <div className={css({ fontWeight: 'bold', mb: 2, color: 'yellow.400', })} > Document Scanner Debug </div> <div className={css({ display: 'flex', flexDirection: 'column', gap: 1, })} > <div> Scanner:{' '} <span className={css({ color: isScannerReady ? 'green.400' : 'orange.400', })} > {isScannerLoading ? 'Loading...' : isScannerReady ? 'Ready' : 'Failed'} </span> </div> <div> Camera:{' '} <span className={css({ color: isReady ? 'green.400' : 'orange.400', })} > {isReady ? 'Ready' : 'Starting...'} </span> </div> <div> Document:{' '} <span className={css({ color: isDetectionLocked ? 'green.400' : isDetectionStable ? 'green.300' : documentDetected ? 'yellow.400' : 'gray.400', })} > {isDetectionLocked ? 'LOCKED' : isDetectionStable ? 'Stable' : documentDetected ? 'Unstable' : 'Not detected'} </span> </div> <div> Quads: {scannerDebugInfo.quadsDetected} detected, {scannerDebugInfo.trackedQuads}{' '} tracked </div> <div> Best: {scannerDebugInfo.bestQuadFrameCount} frames,{' '} {Math.round(scannerDebugInfo.bestQuadStability * 100)}% stable </div> {scannerDebugInfo.loadTimeMs !== null && ( <div>Load time: {scannerDebugInfo.loadTimeMs}ms</div> )} {scannerDebugInfo.lastDetectionMs !== null && ( <div>Detection: {scannerDebugInfo.lastDetectionMs}ms</div> )} {scannerDebugInfo.lastDetectionError && ( <div className={css({ color: 'red.400', wordBreak: 'break-word' })}> Error: {scannerDebugInfo.lastDetectionError} </div> )} </div> </div> <div className={css({ position: 'absolute', bottom: 8, left: '50%', transform: 'translateX(-50%)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3, })} > {/* Helper text for detection status */} <div data-element="detection-status" className={css({ px: 4, py: 2, bg: 'rgba(0, 0, 0, 0.6)', backdropFilter: 'blur(4px)', borderRadius: 'full', color: 'white', fontSize: 'sm', fontWeight: 'medium', textAlign: 'center', transition: 'all 0.2s', })} > {isScannerLoading ? ( 'Loading scanner...' ) : isDetectionLocked ? ( <span className={css({ color: 'green.400', fontWeight: 'bold' })}> ✓ Hold steady - Ready to capture! </span> ) : isDetectionStable ? ( <span className={css({ color: 'green.300' })}> Document detected - Hold steady... </span> ) : documentDetected ? ( <span className={css({ color: 'yellow.400' })}> Detecting... hold camera steady </span> ) : ( 'Point camera at document' )} </div> <button type="button" onClick={capturePhoto} disabled={isCapturing || !isReady} className={css({ width: '80px', height: '80px', bg: 'white', borderRadius: 'full', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: isDetectionLocked ? '0 4px 30px rgba(0, 255, 100, 0.5)' : '0 4px 20px rgba(0, 0, 0, 0.3)', border: '4px solid', borderColor: isDetectionLocked ? 'green.400' : isDetectionStable ? 'green.300' : documentDetected ? 'yellow.400' : 'gray.300', transition: 'all 0.15s', _hover: { transform: 'scale(1.05)' }, _active: { transform: 'scale(0.95)' }, _disabled: { opacity: 0.5, cursor: 'not-allowed' }, })} > {isCapturing ? ( <div className={css({ fontSize: 'sm', color: 'gray.600' })}>...</div> ) : ( <div className={css({ width: '64px', height: '64px', bg: 'white', borderRadius: 'full', border: '2px solid', borderColor: isDetectionLocked ? 'green.400' : isDetectionStable ? 'green.300' : documentDetected ? 'yellow.400' : 'gray.400', })} /> )} </button> </div> {/* Advanced controls drawer - swipe from left edge or click hint to open */} <ScannerControlsDrawer isOpen={isDrawerOpen} onClose={() => setIsDrawerOpen(false)} onOpen={() => setIsDrawerOpen(true)} config={detectorConfig} onConfigChange={handleConfigChange} /> </> )} </div> ) } export default FullscreenCamera |